
var isAround=function(el){
    if(lasttile==-1) return true;
    var i=parseInt(parseInt(el.id)-lasttile);
    console.log("#"+i+$.inArray(i, [1,-1,5,-5]));
    return ($.inArray(i, [1,-1,5,-5])>-1)?true:false;
}
var isWord=function(callback){
    var reponse=false;
    if($("#"+idtilechanged).hasClass('selected')){
    var url='/game/dictionary/?word='+$("#mot").val().toLowerCase();
    console.log(url);
    if(!isUsed($("#mot").val().toUpperCase())){
    $.when($.ajax({
        'url' : url,
        dataType: 'json',
        success: function(json){
            reponse=json['in_dictionary'];
            if(json['in_dictionary']){
                $("#mot").css('background-color','#E9FF91');
                $("#invalidword").hide();
                $("#usedword").hide();
                $("#validword").show();
            }else{
                $("#mot").css('background-color','#FFBD91');
                $("#invalidword").show();
                $("#usedword").hide();
                $("#validword").hide();
            }
            console.log("json['in_dictionary']"+(json['in_dictionary']));    
            
        }})).then(function(data, textStatus, jqXHR){
            if((reponse)&&(typeof callback!='undefined')) callback();
            if((!reponse)&&(typeof callback!='undefined')) location.reload();
        });
    }else{
        $("#mot").css('background-color','#FFBD91');
        $("#invalidword").hide();
        $("#usedword").show();
        $("#validword").hide();
    }
    }else{
        console.log(idtilechanged);
        alert("vous n'avez pas selectionne le nouveau caractere");
    }
}

var isUsed=function(word){
    var i=$.inArray(word, usedwords);
    if (i>=0) return true;
    else return false;
}

var submitTile=function(){
    var newtile='';
    $("input.tile").each(function(){
        newtile+=$(this).val();
    });
    var url=window.location+'play/?word='+$("#mot").val()+'&tile='+newtile;
    console.log(url);
    $.ajax({
        'url' : url,
        dataType: 'json',
        success: function(json){
            location.reload();
        }}).fail(function() {location.reload();});
}

var skip=function(){
    var url=window.location+'skip/';
    $.ajax({
        'url' : url,
        dataType: 'json',
        success: function(json){
            location.reload();
        }}).fail(function() {location.reload();});
}
var finish=function(){
    var url=window.location+'finish/';
    $.ajax({
        'url' : url,
        dataType: 'json',
        success: function(json){
            location.reload();
        }}).fail(function() {location.reload();});
}

var filltile=function(tile){
    $("input.tile").each(function() {
        var id=parseInt(this.id);
        this.value=tile[id];
        if(tile[id]!="."){
            $(this).attr('readonly','readonly');
            $(this).addClass('predefined');
        }   
        });         
    } 

